package hz.mobileproject.pda.presenter;



import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.ui.activity.work.GridChoosePeopleActivity;
import hz.mobileproject.pda.utils.Base64;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class PChoosePeople extends XPresent<GridChoosePeopleActivity> {
    private final String token;
    private Gson gson;
    private Request_data data;
    private String body_data;
    private RequestBody body;


    public PChoosePeople() {
        gson = new Gson();
        data = new Request_data();
        this.token = getV().getApplicationContext().getSharedPreferences("app_login", Context.MODE_PRIVATE).getString("token","");
    }



    public void loadData(Object request_data ,int type){
        String date= gson.toJson(request_data);
        Log.e("load",date);
        data.setData(Base64.getBase64(date));
        body_data=  gson.toJson(data);
        body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),body_data);
        getDataBody(body,type);
    }


    private void getDataBody(RequestBody body,int type) {
        switch (type){
            case 1:    //获取网格化管理事件类型
                Api.getGankService().loadType(body)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().showData(json,1);
                            }
                        });
                break;

            case 2://2.1.12 获取民警信息
                Api.getGankService().getloadPoliceInfo(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                LogUtils.e(error.toString());
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().showData(json,2);
                            }
                        });
                break;
           /* case 3://2.1.12	获取罪犯信息
                Api.getGankService().getloadPrisonerInfo(body)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().showData(json,3);
                            }
                        });
                break;*/
            case 4://  提交
                Api.getGankService().handle(body)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                    getV().showData("",4);

                            }
                        });


                break;

        }

    }

}

